//
//  HTNotificationVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/11/10.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTNotificationVC.h"
#import "HTRefreshMoreModel.h"
#import "HTNotificationCell.h"

@interface HTNotificationVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation HTNotificationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的消息";
    [self configTableView];
    
}
- (void)configTableView{
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.width = kHTScreenWidth;
    self.tableView.backgroundColor = kBackgroundColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTNotificationCell" bundle:nil] forCellReuseIdentifier:NotificationCell];
    self.tableView.estimatedRowHeight = 150;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedSectionHeaderHeight =0;
    self.tableView.estimatedSectionFooterHeight =0;
    [self configTableHeader];
    [self configTableFooter];
    self.tableView.mj_header = self.header;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
    self.noDataView.showMessage = @"暂无消息";
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
#pragma mark --- tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:self.dataSource.count];
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTNotificationCell *cell  = [tableView dequeueReusableCellWithIdentifier:NotificationCell forIndexPath:indexPath];
    cell.notificationModel = self.dataSource[indexPath.row];
    return cell;
}
#pragma mark --- 网络请求
- (void)netLoadData{
    [HTHttpRequestManager requestUserMessageListWithSuccess:^(id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        DBLog(@"消息列表：%@",responseObject);
        if (!responseObject) {
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            return;
        }
        NSArray *result = responseObject[@"data"];
        NSMutableArray *dataResult = [HTNotificationModel mj_objectArrayWithKeyValuesArray:result];
        [self.dataSource addObjectsFromArray:dataResult];
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)loadNewData{
    [self netLoadData];
}

@end
